package com.jbl.te.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.te.entity.Departmentclass;
import com.jbl.te.entity.Major;
import com.jbl.te.entity.vo.DepartmentclassQuery;
import com.jbl.te.entity.vo.DepartmentclassVO;
import com.jbl.te.service.DepartmentclassService;
import com.jbl.te.service.MajorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/te/departmentclass")
@CrossOrigin
public class DepartmentclassController {

    @Autowired
    DepartmentclassService departmentclassService;
    @Autowired
    MajorService majorService;

    @GetMapping("/getAllDepartmentclass")
    public R getAllDepartmentClass() {
        return R.ok().data("list",departmentclassService.list());
    }

    @GetMapping("/getAllDepartmentclassByMajorId")
    public R getAllDepartmentclassByMajorId(String id) {
        QueryWrapper<Departmentclass> wrapper = new QueryWrapper<>();
        wrapper.eq("major_id",id);
        return R.ok().data("list",departmentclassService.list(wrapper));
    }

    @DeleteMapping("delDepartmentclasssByList")
    @ApiOperation("批量删除行政班")
    public R delDepartmentclasssByList(@RequestBody List<String> idList) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//        boolean b = departmentclassService.removeByIds(idList);
//        if(b) {
////            return R.ok();
//        }
//
//        return R.error().message("批量删除失败");
    }


    @PostMapping("{page}/{limit}")
    @ApiOperation("分页行政班列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) DepartmentclassQuery departmentclassQuery) {
        Page<Departmentclass> pageParam = new Page<>(page, limit);
        departmentclassService.pageQuery(pageParam,departmentclassQuery);
        List<Departmentclass> records = pageParam.getRecords();
        List<DepartmentclassVO> list = new ArrayList<>();
        for (Departmentclass departmentclass : records) {
            DepartmentclassVO vo = new DepartmentclassVO();
            BeanUtils.copyProperties(departmentclass,vo);
            Major major = majorService.getById(departmentclass.getMajorId());
            vo.setMajorName(major.getName());
            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }


    @GetMapping("getDepartmentclassById")
    @ApiOperation("根据id查找行政班")
    public R getDepartmentclassById(String id) {
        Departmentclass departmentclass = departmentclassService.getById(id);
        if(departmentclass == null) {
            return R.error().message("行政班不存在");
        }
        DepartmentclassVO vo = new DepartmentclassVO();
        BeanUtils.copyProperties(departmentclass,vo);
        Major major = majorService.getById(departmentclass.getMajorId());
        vo.setMajorName(major.getName());
        return R.ok().data("departmentclass",vo);
    }

    @PutMapping("updateDepartmentclass")
    @ApiOperation("修改行政班")
    public R updateDepartmentclass(@RequestBody Departmentclass departmentclass) {
        boolean b = departmentclassService.updateById(departmentclass);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("修改失败");
        }
    }

    @DeleteMapping("delDepartmentclassById")
    @ApiOperation("删除行政班")
    public R delDepartmentclassById(String id) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//        boolean b = departmentclassService.removeById(id);
//        if(b) {
////            return R.ok();
//        } else {
//            return R.error().message("删除失败");
//        }

    }


    @PostMapping("saveDepartmentclass")
    @ApiOperation("添加新政班")
    public R saveDepartmentclass(@RequestBody Departmentclass departmentclass) {

        boolean b = departmentclassService.save(departmentclass);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }

}

